package ru.jexp.mvc.snippet;

import java.util.Date;

import ru.jexp.mvc.Event;
import ru.jexp.mvc.IListener;
import ru.jexp.mvc.ISubscriber;
import ru.jexp.mvc.SubscriberListener;

public class SnippetView implements IListener, ISubscriber {
	private SubscriberListener listeners = new SubscriberListener();

	public SnippetView() {
		initHandlers();
	}

	public void enterInt(Integer integer) {
		System.out.println("integer entered:" + integer);
		notifyAll(new Event(SnippetEvents.EVENT1, integer, this));
	}

	public void enterDate(Date date) {
		System.out.println("date entered:" + date);
		notifyAll(new Event(SnippetEvents.EVENT2, date, this));
	}

	private void initHandlers() {
		listeners.mapEvent(SnippetEvents.EVENT1, new IListener() {
			public void handle(Event e) {
				System.out.println(toString() + "::Handle(" + e.type
						+ ") set integer=" + e.data);
			}
		});
		listeners.mapEvent(SnippetEvents.EVENT2, new IListener() {
			public void handle(Event e) {
				System.out.println(toString() + "::Handle(" + e.type
						+ ") set date=" + e.data);
			}
		});

	}

	public void handle(Event e) {
		listeners.handle(e);
	}

	public void addListener(IListener listener) {
		listeners.addListener(listener);
	}

	public void notifyAll(Event e) {
		listeners.notifyAll(e);
	}

	public void removeListener(IListener listener) {
		listeners.removeListener(listener);
	}
}
